//
//  HomeList.swift
//  DesignCode
//
//  Created by Json on 2020/4/11.
//  Copyright © 2020 Json. All rights reserved.
//

import SwiftUI

struct HomeList: View {
    @State var showModel = false
    var courses = courseData
    var body: some View {
        ScrollView {
            VStack {
                
                HStack {
                    VStack(alignment: .leading) {
                        Text("Courses")
                            .font(.largeTitle)
                            .fontWeight(.heavy)
                        Text("22 courses")
                            .foregroundColor(Color.gray)
                    }
                    Spacer()
                }
                .padding(.leading, 60.0)
                .padding(.bottom, 40)
                
                ScrollView(Axis.Set.horizontal, showsIndicators: false) {
                    HStack(spacing: 30) {
                        ForEach(courses) { item in
                            Button(action: {
                                self.showModel = true
                            }){ //从几何阅读器中得到CourseView视图，
                                GeometryReader { geometry in
                                    CourseView(title: item.title, image: item.image, background: item.color, shadow: item.shadowColor)
                                        //在全局内获得框架，取最小的x值，最小的x值是看框架开始的位置，最大h值是框架结束位置，框架最小位置会随着滚动而改变
                                        .rotation3DEffect(Angle(degrees:
                                            Double(geometry.frame(in: .global).minX - 30) / -40
                                        ), axis: (x: 0, y: 10.0, z: 0)) // 根据选择的轴来做旋转
                                }
                                .frame(width: 246, height: 360)
                            }.sheet(isPresented: self.$showModel){
                                ContentView()
                            }
                        }
                    }
                    .padding(.leading, 30)
                    .padding(.top, 30)
                    .padding(.bottom, 70)
                    Spacer()
                }
                CertificateRow()
            }
            .padding(.top, 78.0)
        }
    }
}

struct HomeList_Previews: PreviewProvider {
    static var previews: some View {
        HomeList()
    }
}

struct CourseView: View {
    var title = "Build an app with SwiftUI"
    var image = "Illustration1"
    var background = Color("background3")
    var shadow = Color("backgroundshadow3")
    var body: some View {
        VStack(alignment: .leading) {
            Text(title)
                .font(.title)
                .fontWeight(.bold)
                .foregroundColor(.white)
                .padding(30)
                .lineLimit(4)
                .padding(.trailing,50)
            Spacer()
            Image(image)
                .resizable() //缩放
                .renderingMode(.original) //渲染模式，渲染模式要放在纵横比前
                .aspectRatio(contentMode: .fit) //纵横比
                .frame(width: 246, height: 150)
                .padding(.bottom,30)
        }
        .background(background)
        .cornerRadius(30)
        .frame(width: 246, height: 360)
        .shadow(color: shadow, radius: 20, x: 0, y: 20)
    }
}

struct Course : Identifiable {
    var id = UUID()
    var title: String
    var image: String
    var color: Color
    var shadowColor: Color
}

let courseData = [
    Course(title: "Build an app with SwiftUI",
           image: "Illustration1",
           color: Color("background3"),
           shadowColor: Color("backgroundshadow3")),
    Course(title: "Design Course",
           image: "Illustration2",
           color: Color("background4"),
           shadowColor: Color("backgroundshadow4")),
    Course(title: "Build an app with SwiftUI",
           image: "Illustration1",
           color: Color("background3"),
           shadowColor: Color("backgroundshadow3")),
    Course(title: "Design Course",
           image: "Illustration2",
           color: Color("background4"),
           shadowColor: Color("backgroundshadow4")),
    Course(title: "Build an app with SwiftUI",
           image: "Illustration1",
           color: Color("background3"),
           shadowColor: Color("backgroundshadow3")),
    Course(title: "Design Course",
           image: "Illustration2",
           color: Color("background4"),
           shadowColor: Color("backgroundshadow4"))
]
